package integral

import (
	"context"
	"fmt"
	"private-domain-overseas-service/admin/business/api/internal/repository/integral"
	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"
	"private-domain-overseas-service/pkg/status/bizintegral"
	"private-domain-overseas-service/pkg/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type IntegralGoodsOrderEditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 积分消费订单发货设置
func NewIntegralGoodsOrderEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IntegralGoodsOrderEditLogic {
	return &IntegralGoodsOrderEditLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IntegralGoodsOrderEditLogic) IntegralGoodsOrderEdit(req *types.IntegralGoodsOrderEditRequest) error {
	integralOrderDao := integral.NewIntegralOrderDao(l.ctx, l.svcCtx)

	//长度校验
	if len(req.GoodsSecret) > 200 {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("密钥长度不能超过200"))
	}
	goodsSecret := req.GoodsSecret
	//如果需要讲状态设置为未发货则置空商品密钥
	if req.OrderStatus == bizintegral.UnReceived {
		goodsSecret = ""
	}
	
	//修改订单状态
	err := integralOrderDao.IntegralOrderEditStatus(int(req.Id), req.OrderStatus, goodsSecret, nil)
	if err != nil {
		return xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, fmt.Sprintf("设置商品发货状态失败：%s", err.Error()))
	}

	return nil
}
